<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px" size="small">
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="请输入用户名" size="small" style="width:200px"></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row style="margin-bottom:10px;">
      <el-button type="primary" size="mini" @click="handleAddUser">新增</el-button>
      <el-button type="danger" size="mini" @click="handleDeleteUser">删除</el-button>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="用户名">
        <template slot-scope="scope">{{ scope.row.username }}</template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">{{ scope.row.descr }}</template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">查看</el-button> -->
          <el-button size="mini" @click="view(scope.row)">查看</el-button>
          <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="pageSizes"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
    ></el-pagination>

    <!-- 用户新增 -->
    <UserAdd
      @success="onSuccess"
      @failed="onFailed($event)"
      @cancel="onCancel"
      @close="onClose"
      :userAddDialogVisible="userAdd.visible"
    ></UserAdd>

    <!-- 用户编辑 -->
    <UserEdit
      ref="userEdit"
      @success="handleEditSuccess"
      @failed="handleEditFailed($event)"
      @cancel="handleEditCancel"
      @close="handleEditClose"
      :userEditDialogVisible="userEdit.visible"
    ></UserEdit>

    <!-- 用户查看 -->
    <UserView
      ref="userView"
      @cancel="handleViewClose"
      @close="handleViewClose"
      :userViewDialogVisible="userView.visible"
    ></UserView>
  </div>
</template>

<script>
import UserAdd from "@/views/system/user/UserAdd";
import UserEdit from "@/views/system/user/UserEdit";
import UserView from "@/views/system/user/UserView";
import { getList, deleteUsers } from "@/api/user";

export default {
  name: "User",
  components: { UserAdd, UserEdit, UserView },
  data() {
    return {
      list: null,
      listLoading: true,
      form: {
        username: "",
      },
      userAdd: {
        visible: false,
      },
      userEdit: {
        visible: false,
      },
      userView: {
        visible: false,
      },
      pageSizes: [10, 20, 30, 40],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      mutipleSelection: [],
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      var params = {
        username: this.form.username,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      };

      getList(params).then((response) => {
        if (response.data) {
          this.list = response.data.users;
          this.total = response.data.total;
        } else {
          this.list = [];
          this.total = 0;
        }
        this.listLoading = false;
      });
    },
    // handleEditRole(index, row) {
    //   console.log(index, row);
    // },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.fetchData();
    },
    handleSearch() {
      this.fetchData();
    },
    handleSelectionChange(val) {
      console.log(val);
      this.mutipleSelection = val;
    },
    handleAddUser() {
      this.userAdd.visible = true;
      console.log("---visible--");
      console.log("---this.userAdd.visible--" + this.userAdd.visible);
    },
    handleDeleteUser() {
      // this.addUserDialogVisible=true
      if (this.mutipleSelection.length > 0) {
        this.$confirm("确定要删除吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            var ids = [];
            for (let i = 0; i < this.mutipleSelection.length; i++) {
              ids.push(this.mutipleSelection[i].id);
            }

            deleteUsers({ ids: ids.join(",") }).then((response) => {
              if (response.code == "200") {
                this.$message.success("操作成功");
                this.fetchData();
              } else {
                this.$message.error("操作失败");
              }
              // this.listLoading = false;
            });
            // this.$message.success("操作成功");
          })
          .catch(() => {
            this.$message.info("已取消");
          });
      } else {
        this.$message.error("请选择要删除的用户");
      }
    },
    onSuccess() {
      this.userAdd.visible = false;
      // 添加成功
      this.$message({
        message: "新增用户成功！",
        type: "success",
      });
      this.fetchData();
    },
    onFailed(msg) {
      this.userAdd.visible = false;
      this.$message.error("新增用户失败！" + msg);
    },
    onCancel() {
      this.userAdd.visible = false;
    },
    onClose() {
      console.log("close");
      this.userAdd.visible = false;
    },
    edit(record) {
      console.log(record);
      this.userEdit.visible = true;
      this.$refs.userEdit.setFormValues(record);
    },
    handleEditSuccess() {
      this.userEdit.visible = false;
      this.$message({
        message: "修改用户成功！",
        type: "success",
      });
      this.fetchData();
    },
    handleEditFailed(msg) {
      this.userEdit.visible = false;
      this.$message.error("修改用户失败！" + msg);
    },
    handleEditCancel() {
      this.userEdit.visible = false;
    },
    handleEditClose() {
      console.log("close");
      this.userEdit.visible = false;
    },
    view(record) {
      console.log(record);
      this.userView.visible = true;
      this.$refs.userView.setFormValues(record);
    },
    handleViewClose() {
      console.log("close");
      this.userView.visible = false;
    },
  },
};
</script>
